/**
 * 
 */
package org.xlab.baseframework.test.platform.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;
import org.xlab.baseframework.exception.ObjectNotFoundException;
import org.xlab.baseframework.test.platform.dao.api.IBizTransactionLogDAO;
import org.xlab.baseframework.test.platform.domain.BizTransactionLog;
import org.xlab.baseframework.test.platform.domain.EpcCache.EpcInfo;
import org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService;

/**
 * @author makubex
 * 
 */
public class BizTransactionLogServiceImpl implements IBizTransactionLogService {

	private IBizTransactionLogDAO bizTransactionLogDAO;

	/**
	 * @see org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService#checkLogic(org.xlab.baseframework.test.platform.domain.BizTransactionLog)
	 */
	public boolean checkLogic(BizTransactionLog bizLog,
			Map<String, EpcInfo> ivnImage) {
		EpcInfo epcInfo = ivnImage.get(bizLog.getEpcNo());
		if (epcInfo == null
				|| epcInfo.getDeletedInd() == EnumDeletedIndentifying.Y) {
			if (bizLog.getCommandId().equalsIgnoreCase("PA"))
				return true;
			else
				return false;
		} else if (epcInfo != null
				&& epcInfo.getDeletedInd() == EnumDeletedIndentifying.N) {
			if (bizLog.getCommandId().equalsIgnoreCase("PLACE")
					&& (epcInfo.getLastOperation().equalsIgnoreCase("PICK") || epcInfo
							.getLastOperation().equalsIgnoreCase("PA")))
				return true;
			else if (bizLog.getCommandId().equalsIgnoreCase("PICK")
					&& (epcInfo.getLastOperation().equalsIgnoreCase("PLACE") || epcInfo
							.getLastOperation().equalsIgnoreCase("PA"))
					&& bizLog.getLocation().equalsIgnoreCase(
							epcInfo.getCurrentLocation()))
				return true;
			else if (bizLog.getCommandId().equalsIgnoreCase("GI")
					&& epcInfo.getLastOperation().equalsIgnoreCase("PLACE"))
				return true;
			else
				return false;
		} else {
			return false;
		}
	}

	/**
	 * @throws ObjectNotFoundException
	 * @see org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService#findAllByExample(org.xlab.baseframework.test.platform.domain.BizTransactionLog)
	 */
	public List<BizTransactionLog> findAllByExample(
			BizTransactionLog bizTransactionLog) throws ObjectNotFoundException {
		return bizTransactionLogDAO.findAllByExample(bizTransactionLog);
	}

	/**
	 * @see org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService#findAllByCondition(java.util.Date)
	 */
	public List<BizTransactionLog> findAllByMaxCreatedTime(Date maxCreatedTime)
			throws ObjectNotFoundException {
		return bizTransactionLogDAO.findAllByMaxCreatedTime(maxCreatedTime);
	}

	/**
	 * @return the bizTransactionLogDAO
	 */
	public IBizTransactionLogDAO getBizTransactionLogDAO() {
		return bizTransactionLogDAO;
	}

	/**
	 * @see org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService#save(org.xlab.baseframework.test.platform.domain.BizTransactionLog)
	 */
	public void save(BizTransactionLog bizTransactionLog) {
		bizTransactionLogDAO.save(bizTransactionLog);
	}

	/**
	 * @param bizTransactionLogDAO
	 *            the bizTransactionLogDAO to set
	 */
	public void setBizTransactionLogDAO(
			IBizTransactionLogDAO bizTransactionLogDAO) {
		this.bizTransactionLogDAO = bizTransactionLogDAO;
	}

	/**
	 * @see org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService#update(org.xlab.baseframework.test.platform.domain.BizTransactionLog)
	 */
	public void update(BizTransactionLog bizTransactionLog) {
		bizTransactionLogDAO.update(bizTransactionLog);
	}

}
